//
//  ZuzhiHistoryCell.m
//  ZhuoxinAgriculture
//
//  Created by 左忠飞 on 16/11/11.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "ZuzhiHistoryCell.h"
#import "Masonry.h"

@interface ZuzhiHistoryCell ()
@property (nonatomic, strong) UIView *historyView;
@property (nonatomic, strong) UILabel *historyLabel;
@property (nonatomic, strong) NSArray <UIButton *>*btnArraysTop;
@property (nonatomic, strong) NSArray <UIButton *>*btnArraysBtm;
@end

@implementation ZuzhiHistoryCell

+ (instancetype)historyCellWithTableView:(UITableView *)tableView {
    ZuzhiHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellId"];
    if (!cell)
    {
        cell = [[ZuzhiHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"historyCellId"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIView *wrapperTop = [UIView new];
        UIView *wrapperBtm = [UIView new];
        for (NSInteger i = 0;i<self.btnArraysTop.count;i++) {
            [wrapperTop addSubview:self.btnArraysTop[i]];
        }
        for (NSInteger i = 0;i<self.btnArraysBtm.count;i++) {
            [wrapperBtm addSubview:self.btnArraysBtm[i]];
        }
        
        [self.contentView addSubview:self.historyView];
        [self.contentView addSubview:wrapperBtm];
        [self.contentView addSubview:wrapperTop];
        [self.historyView addSubview:self.historyLabel];
        
        
        [self.historyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.historyView).offset(kI(36.0));
            make.left.mas_equalTo(self.historyView).offset(kI(48.0));
        }];
        
        
        [self.historyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.contentView);
        }];
        
        [wrapperTop mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.historyLabel.mas_bottom).offset(kI(36.0));
            make.leading.mas_equalTo(self.historyLabel);
            make.trailing.mas_equalTo(self.contentView).offset(-kI(48.0));
            make.height.mas_equalTo(kI(60.0));
        }];
        
        [wrapperBtm mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(wrapperTop.mas_bottom).offset(kI(36.0));
            make.leading.mas_equalTo(self.historyLabel);
            make.trailing.mas_equalTo(wrapperTop);
            make.height.mas_equalTo(kI(60.0));
        }];
        
        [self.btnArraysTop mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kI(32.0) leadSpacing:0 tailSpacing:0];
        [self.btnArraysTop mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(wrapperTop);
            make.bottom.mas_equalTo(wrapperTop);
        }];
        
        [self.btnArraysBtm mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kI(32.0) leadSpacing:0 tailSpacing:0];
        [self.btnArraysBtm mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(wrapperBtm);
            make.bottom.mas_equalTo(wrapperBtm);
        }];
        
    }
    return self;
}

- (UILabel *)historyLabel
{
    if (!_historyLabel)
    {
        _historyLabel = [UILabel new];
        _historyLabel.text = @"历史搜索";
        _historyLabel.font = [UIFont systemFontOfSize:kI(28)];
        _historyLabel.textColor = UIColorFromRGBA(0x7d7c82);
    }
    return _historyLabel;
}

- (UIView *)historyView
{
    if (!_historyView) {
        _historyView = [UIView new];
    }
    return _historyView;
}

- (NSArray<UIButton *> *)btnArraysTop
{
    if (!_btnArraysTop) {
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSInteger i = 0;i<4;i++) {
            UIButton *btn = [self historyBtn];
            btn.tag = i;
            [arr addObject:btn];
        }
        _btnArraysTop = [arr copy];
    }
    return _btnArraysTop;
}

- (NSArray<UIButton *> *)btnArraysBtm
{
    if (!_btnArraysBtm) {
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSInteger i = 0;i<4;i++) {
            UIButton *btn = [self historyBtn];
            btn.tag = i+4;
            [arr addObject:btn];
        }
        _btnArraysBtm = [arr copy];
    }
    return _btnArraysBtm;
}

- (UIButton *)historyBtn
{
    UIButton *btn = [UIButton new];
    btn.layer.borderWidth = 1.0;
    btn.layer.borderColor = UIColorFromRGBA(0xafafaf).CGColor;
    [btn setTitleColor:UIColorFromRGBA(0x7d7c82) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:kI(24.0)];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.cornerRadius = 2;
    btn.layer.masksToBounds = YES;
    return btn;
}

- (void)setHistoryLabels:(NSArray<NSString *> *)historyLabels
{
    for (NSInteger i = 0;i<historyLabels.count;i++) {

        if (i >= 4) {
            [self.btnArraysBtm[i-4] setTitle:historyLabels[i] forState:UIControlStateNormal];
        } else {
            [self.btnArraysTop[i] setTitle:historyLabels[i] forState:UIControlStateNormal];
        }
    }
}

- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%@", btn.currentTitle);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
